<script setup lang="ts">
import { reactive, ref, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import { ActivityAxios } from "@/api";
import moment from "moment";
import { UserStore } from "@/store";
const userStore = UserStore();
const { t } = useI18n();
const selctList = reactive([
  t("activity.distriAgent.today"),
  t("activity.distriAgent.yesterday"),
  t("activity.distriAgent.thisWeek"),
  t("activity.distriAgent.lastWeek"),
  t("activity.distriAgent.month"),
  t("activity.distriAgent.lastMonth"),
]);
const scrollContainer = ref();
const listIndex = ref(0);
const getTime = ref("");
onMounted(() => {
  //获取无限代理总数据
  if (userStore.isLogin) {
    getUnlimitedAgentTotalInfo(1);
  }
  const getTimeData = getTimeRange(1);
  getTime.value = `${getTimeData.start} - ${getTimeData.end}`;
});
const selectTitle = (index, event) => {
  //获取时间
  const getTimeData = getTimeRange(index + 1);
  getTime.value = `${getTimeData.start} - ${getTimeData.end}`;
  //获取无限代理总数据
  if (userStore.isLogin) {
    getUnlimitedAgentTotalInfo(index + 1);
  }
  //判断点击当前的
  const clickedDiv = event.currentTarget;
  if (scrollContainer.value && isOverflowing(clickedDiv)) {
    adjustScrollPosition(clickedDiv);
  }
  listIndex.value = index;
  // 获取父级div的滚动容器
  const parent = scrollContainer.value;
  const prevSibling = event.target.previousElementSibling;
  const nextSibling = event.target.nextElementSibling as HTMLElement | null;
  const parentRect = event.target.parentElement?.getBoundingClientRect();
  // 判断上一个兄弟元素是否溢出
  if (prevSibling) {
    const prevRect = prevSibling.getBoundingClientRect();
    if (prevRect.left < parentRect.left) {
      //下一个兄弟元素溢出，向左滚动
      const scrollAmount = parent.scrollLeft + (prevRect.left - parentRect.left);
      parent.scrollLeft = scrollAmount; // 向右滚动
    }
  }

  // 判断下一个兄弟元素是否溢出
  if (nextSibling) {
    const nextRect = nextSibling.getBoundingClientRect();
    if (nextRect.right > parentRect.right) {
      //下一个兄弟元素溢出，向左滚动
      const scrollAmount = parent.scrollLeft + (nextRect.right - parentRect.right);
      parent.scrollLeft = scrollAmount; // 向左滚动
    }
  }
};
const adjustScrollPosition = clickedDiv => {
  const containerRect = scrollContainer.value.getBoundingClientRect();
  const divRect = clickedDiv.getBoundingClientRect();

  if (divRect.left < containerRect.left) {
    // 如果点击的div在可见区域左侧
    scrollContainer.value.scrollLeft -= containerRect.left - divRect.left;
  } else if (divRect.right > containerRect.right) {
    // 如果点击的div在可见区域右侧
    scrollContainer.value.scrollLeft += divRect.right - containerRect.right;
  }
};
const isOverflowing = div => {
  const divRect = div.getBoundingClientRect();
  const containerRect = scrollContainer.value.getBoundingClientRect();
  return !(divRect.left >= containerRect.left && divRect.right <= containerRect.right);
};

const totalInfoData = ref({
  totalBet: 0,
  totalCommissions: 0,
  totalFirstDeposit: 0,
  totalRegisterPeopleNum: 0,
});
//获取无限代理总数据
const getUnlimitedAgentTotalInfo = type => {
  ActivityAxios.getUnlimitedAgentTotalInfo({ timeRangeType: type }).then(res => {
    totalInfoData.value = res;
  });
};
//获取时间的方法
function getTimeRange(type) {
  const format = "YYYY.MM.DD HH:mm";
  switch (type) {
    case 1:
      return {
        start: moment().startOf("day").add(8, "hours").format(format),
        end: moment().add(1, "days").startOf("day").add(8, "hours").format(format),
      };
    case 2:
      return {
        start: moment().subtract(1, "days").startOf("day").add(8, "hours").format(format),
        end: moment().subtract(1, "days").endOf("day").add(8, "hours").format(format),
      };
    case 3:
      return {
        start: moment().startOf("week").add(8, "hours").format(format),
        end: moment().endOf("week").add(8, "hours").format(format),
      };
    case 4:
      return {
        start: moment().subtract(1, "weeks").startOf("week").add(8, "hours").format(format),
        end: moment().subtract(1, "weeks").endOf("week").add(8, "hours").format(format),
      };
    case 5:
      return {
        start: moment().startOf("month").add(8, "hours").format(format),
        end: moment().endOf("month").add(8, "hours").format(format),
      };
    case 6:
      return {
        start: moment().subtract(1, "months").startOf("month").add(8, "hours").format(format),
        end: moment().subtract(1, "months").endOf("month").add(8, "hours").format(format),
      };
    default:
      throw new Error("Invalid range");
  }
}
</script>
<template>
  <div class="myData-frame">
    <div ref="scrollContainer" class="select-list-frame df ai-center">
      <div
        v-for="(item, index) in selctList"
        :key="index"
        class="select-frame df ai-center"
        :class="{ 'sure-select': index == listIndex }"
        @click="selectTitle(index, $event)"
      >
        {{ item }}
      </div>
    </div>
    <div class="time-frame df ai-center">
      <img class="yuce-bai-frame" src="@/image/new_img/activity/yuce_bai.png" />
      <div class="time-text">{{ getTime }}</div>
    </div>
    <div class="list-frame">
      <div class="list-content">
        <img class="kapian-img" src="@/image/new_img/activity/dados_kapian.png" />
        <div class="content-frame df ai-center">
          <div class="content-div df ai-center jc-space-between">
            <div class="title-name">{{ t("activity.distriAgent.totalCommissio") }}</div>
            <div class="money-frame df ai-center">
              {{ t("currencyType.pt") }}
              <div class="money-val">{{ totalInfoData.totalCommissions }}</div>
            </div>
          </div>
          <div class="content-div df ai-center jc-space-between">
            <div class="title-name">{{ t("activity.distriAgent.totalBets") }}</div>
            <div class="money-frame df ai-center">
              {{ t("currencyType.pt") }}
              <div class="money-val">{{ totalInfoData.totalBet }}</div>
            </div>
          </div>
          <div class="content-div df ai-center jc-space-between">
            <div class="title-name">{{ t("activity.distriAgent.registration") }}</div>
            <div class="money-frame df ai-center">
              <div class="money-val">{{ totalInfoData.totalRegisterPeopleNum }}</div>
              <span class="reigth10">{{ t("activity.treasureChest.people") }}</span>
            </div>
          </div>
          <div class="content-div df ai-center jc-space-between">
            <div class="title-name">{{ t("activity.distriAgent.totalInitial") }}</div>
            <div class="money-frame df ai-center">
              <div class="money-val">{{ totalInfoData.totalFirstDeposit }}</div>
              <span class="reigth10">{{ t("activity.treasureChest.people") }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="less" scoped>
.myData-frame {
  width: 100%;
  height: auto;
  margin-top: 42px;
  .select-list-frame {
    width: 100%;
    height: auto;
    overflow-x: auto;
    .select-frame {
      font-size: 21px;
      padding-left: 19.77px;
      padding-right: 19.77px;
      height: 68px;
      border-radius: 14.498px;
      background: rgba(255, 255, 255, 0.08);
      color: rgba(255, 255, 255, 0.85);
      margin-right: 14.498px;
      white-space: nowrap;
    }
    .sure-select {
      background-image: linear-gradient(57deg, #fe7919 0%, #ab3fe7 100%);
    }
  }
  .select-list-frame::-webkit-scrollbar {
    display: none;
    overflow: -moz-scrollbars-none;
    /* Firefox */
    -ms-overflow-style: none;
    /* IE 和 Edge */
  }
  .time-frame {
    width: 100%;
    height: 100px;
    background-image: linear-gradient(
      90deg,
      rgba(255, 255, 255, 0) 0%,
      rgba(255, 255, 255, 0.25) 100%
    );
    border-radius: 14.498px;
    margin-top: 14.498px;
    .yuce-bai-frame {
      width: 42px;
      height: 42px;
      opacity: 0.45;
    }
    .time-text {
      font-weight: 400;
      font-size: 28px;
      color: #ffffff;
      margin-left: 16px;
    }
  }
  .list-frame {
    width: 100%;
    height: auto;
    margin-top: 14.498px;
    .list-content {
      width: 100%;
      height: auto;
      position: relative;
      left: 0;
      top: 0;
      margin-bottom: 14.498px;
      .kapian-img {
        width: 100%;
        height: 406px;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
      }
      .content-frame {
        width: 100%;
        height: auto;
        position: absolute;
        left: 0;
        top: 0;
        z-index: 1;
        display: flex;
        flex-direction: column; /* 子级垂直排列 */
      }
      .content-div {
        width: calc(100% - 56px);
        margin: auto;
        height: 100px;
        .title-name {
          color: #ffffffd9;
          font-size: 28px;
          font-weight: 700;
        }
        .money-frame {
          font-size: 24px;
          color: rgba(255, 255, 255, 0.45);
        }
        .money-val {
          color: #ffffff;
          font-size: 36px;
          margin-left: 7.9px;
          font-weight: 700;
        }
      }
    }
  }
}
.reigth10 {
  margin-left: 10px;
}
</style>
